#include "lolevel.h"
#include "platform.h"
#include "core.h"

const char * const new_sa = &_end;

/* Ours stuff */
extern long wrs_kernel_bss_start;
extern long wrs_kernel_bss_end;

// Forward declarations
void CreateTask_PhySw();
void CreateTask_spytask();

void boot();

#define DEBUG_LED 0xC02200C4

void boot() { 
    long *canon_data_src = (void*)0xFFEAE4FC;
    long *canon_data_dst = (void*)0x1900;
    long canon_data_len = 0x12A84 - 0x1900; // data_end - data_start
    long *canon_bss_start = (void*)0x12A84; // just after data 
    long canon_bss_len = 0xA3F20 - 0x12A84; 

    long i;


    asm volatile (
	"MRC     p15, 0, R0,c1,c0\n"
	"ORR     R0, R0, #0x1000\n"
	"ORR     R0, R0, #4\n"
	"ORR     R0, R0, #1\n"
	"MCR     p15, 0, R0,c1,c0\n"
    :::"r0");

    for(i=0;i<canon_data_len/4;i++)
	canon_data_dst[i]=canon_data_src[i];

    for(i=0;i<canon_bss_len/4;i++)
	canon_bss_start[i]=0;

    asm volatile ("B      sub_FFC001A4_my\n");
}; 



void __attribute__((naked,noinline)) sub_FFC001A4_my() { 
        asm volatile (
                "LDR     R0, =0xFFC0021C\n"
                "MOV     R1, #0\n"
                "LDR     R3, =0xFFC00254\n"

        "loc_FFC001B0:\n"
                "CMP     R0, R3\n"
                "LDRCC   R2, [R0],#4\n"
                "STRCC   R2, [R1],#4\n"
                "BCC     loc_FFC001B0\n"

                "LDR     R0, =0xFFC00254\n"
                "MOV     R1, #0x4B0\n"
                "LDR     R3, =0xFFC00468\n"

        "loc_FFC001CC:\n"
                "CMP     R0, R3\n"
                "LDRCC   R2, [R0],#4\n"
                "STRCC   R2, [R1],#4\n"
                "BCC     loc_FFC001CC\n"
                "MOV     R0, #0xD2\n"
                "MSR     CPSR_cxsf, R0\n"
                "MOV     SP, #0x1000\n"
                "MOV     R0, #0xD3\n"
                "MSR     CPSR_cxsf, R0\n"
                "MOV     SP, #0x1000\n"
                "LDR     R0, =0x6C4\n"
                "LDR     R2, =0xEEEEEEEE\n"
                "MOV     R3, #0x1000\n"

        "loc_FFC00200:\n"
                "CMP     R0, R3\n"
                "STRCC   R2, [R0],#4\n"
                "BCC     loc_FFC00200\n"

                "BL      sub_FFC00FB8_my\n"        //----------------->
        );
} 

void __attribute__((naked,noinline)) sub_FFC00FB8_my() { 
        asm volatile (
              "STR     LR, [SP,#-4]!\n"         
              "SUB     SP, SP, #0x74\n"
              "MOV     R0, SP\n"
              "MOV     R1, #0x74\n"
              "BL      sub_FFE4A784\n"
              "MOV     R0, #0x53000\n"
              "STR     R0, [SP,#4]\n"
        );
//              "LDR     R0, =0xD3F20\n"         // new heap start: 0xA3F20+0x30000
        asm volatile (
              "LDR     R0, =new_sa\n"
              "LDR     R0, [R0]\n"
        );
        asm volatile (
              "LDR     R2, =0x2ABC00\n"        // DRY_SYS_MEM_START
              "LDR     R1, =0x2A4968\n"        // heap end
              "STR     R0, [SP,#8]\n"
              "SUB     R0, R1, R0\n"
              "ADD     R3, SP, #0xC\n"
              "STR     R2, [SP]\n"
              "STMIA   R3, {R0-R2}\n"
              "MOV     R0, #0x22\n"
              "STR     R0, [SP,#0x18]\n"
              "MOV     R0, #0x68\n"
              "STR     R0, [SP,#0x1C]\n"
              "LDR     R0, =0x19B\n"
              "MOV     R1, #0x64\n"
              //"STRD     R0, [SP,#0x20]\n"        // "strd not supported by cpu" claims gcc
              "STR      R0, [SP,#0x20]\n"          // split in two single-word STRs
              "STR      R1, [SP,#0x24]\n"

              "MOV     R0, #0x78\n"
              //"STRD     R0, [SP,#0x28]\n"        // "strd not supported by cpu" claims gcc
              "STR      R0, [SP,#0x28]\n"          // split in two single-word STRs
              "STR      R1, [SP,#0x2C]\n"

              "MOV     R0, #0\n"
              "STR     R0, [SP,#0x30]\n"
              "STR     R0, [SP,#0x34]\n"
              "MOV     R0, #0x10\n"
              "STR     R0, [SP,#0x5C]\n"
              "MOV     R0, #0x800\n"
              "STR     R0, [SP,#0x60]\n"
              "MOV     R0, #0xA0\n"
              "STR     R0, [SP,#0x64]\n"
              "MOV     R0, #0x280\n"
              "STR     R0, [SP,#0x68]\n"

              "LDR     R1, =uHwSetup_my\n"         //---------------------->

              "MOV     R0, SP\n"
              "MOV     R2, #0\n"
              "BL      sub_FFC02D70\n"
              "ADD     SP, SP, #0x74\n"
              "LDR     PC, [SP],#4\n"
        );
}; 

void __attribute__((naked,noinline)) uHwSetup_my() { 
        asm volatile (
              "STMFD   SP!, {R4,LR}\n"
              "BL      sub_FFC0095C\n"
              "BL      sub_FFC09948\n"         
              "CMP     R0, #0\n"
              "LDRLT   R0, =0xFFC04ED0\n"       
              "BLLT    sub_FFC04EB0\n"          
              "BL      sub_FFC049E0\n"          
              "CMP     R0, #0\n"
              "LDRLT   R0, =0xFFC04ED8\n"       
              "BLLT    sub_FFC04EB0\n"          
              "LDR     R0, =0xFFC04EE8\n"      
              "BL      sub_FFC04ACC\n"         
              "CMP     R0, #0\n"
              "LDRLT   R0, =0xFFC04EF0\n"      
              "BLLT    sub_FFC04EB0\n"         
              "LDR     R0, =0xFFC04EE8\n"      
              "BL      sub_FFC0357C\n"         
              "CMP     R0, #0\n"
              "LDRLT   R0, =0xFFC04F04\n"      
              "BLLT    sub_FFC04EB0\n"         
              "BL      sub_FFC094D0\n"         
              "CMP     R0, #0\n"
              "LDRLT   R0, =0xFFC04F10\n"      
              "BLLT    sub_FFC04EB0\n"         
              "BL      sub_FFC014D0\n"         
              "CMP     R0, #0\n"
              "LDRLT   R0, =0xFFC04F1C\n"      
              "BLLT    sub_FFC04EB0\n"         
              "LDMFD   SP!, {R4,LR}\n"
              "B       CreateTask_Startup_my\n" //------------------>

        );
}; 

void __attribute__((naked,noinline)) CreateTask_Startup_my() {  
        asm volatile (
                "STMFD   SP!, {R3,LR}\n"
              //"BL      nullsub_3\n"
                "BL      sub_FFC1BB7C\n"
                "CMP     R0, #0\n"
                "LDREQ   R0, =0xC0220000\n"
                "LDREQ   R1, [R0,#0xB8]\n"
                "TSTEQ   R1, #1\n"
                "BNE     loc_FFC0DC38\n"
                "MOV     R1, #0x44\n"
                "STR     R1, [R0,#0x4C]\n"
        "loc_FFC0DC34:\n"
                "B       loc_FFC0DC34\n"
        "loc_FFC0DC38:\n"
              //"BL      nullsub_5\n"
              //"BL      nullsub_4\n"
                "BL      sub_FFC19708\n"
                "MOV     R1, #0x300000\n"
                "MOV     R0, #0\n"

                "BL      sub_FFC19950\n"
                "BL      sub_FFC198FC\n"
                "MOV     R3, #0\n"
                "STR     R3, [SP]\n"

                "LDR     R3, =task_Startup_my\n"  //-------------------->

                "MOV     R2, #0\n"
                "MOV     R1, #0x19\n"
                "LDR     R0, =0xFFC0DC7C\n"      // "Startup" 	
                "BL      sub_FFC0BAF0\n"         // CreateTask
                "MOV     R0, #0\n"
                "LDMFD   SP!, {R12,PC}\n"
        );
}; 

void __attribute__((naked,noinline)) task_Startup_my() { 
        
        asm volatile (
                "STMFD   SP!, {R4,LR}\n"   
                "BL      sub_FFC0517C\n"    
                "BL      sub_FFC13E9C\n"
                "BL      sub_FFC10D98\n"
                "BL      sub_FFC1BBBC\n"
                "BL      sub_FFC1BD5C\n"
              //"BL      sub_FFC1BC44\n"  // StartDiskboot
        );

        CreateTask_spytask();

        asm volatile (
                "BL      sub_FFC1BF24\n"
                "BL      sub_FFC1BDAC\n"        
                "BL      sub_FFC18C4C\n"
                "BL      sub_FFC1BF28\n"
        );

        CreateTask_PhySw();

        asm volatile (
             //"BL      sub_FFC12CA0\n"        // CreateTask PhySw()

                "BL      sub_FFC15BA4_my\n"   //-------------------> 
                "BL      sub_FFC1BF40\n"
              //"BL      nullsub_2\n"
                "BL      sub_FFC12030\n"
                "BL      sub_FFC1B928\n"
                "BL      sub_FFC127CC\n"
                "BL      sub_FFC11F3C\n"
                "BL      sub_FFC1C888_my\n"   //-------------->  divert for SDHC-bootdisk-support
                "BL      sub_FFC11EF8\n"

                // modification: BL instead of B to last function to control action after its return
                "BL       sub_FFC05088\n"
                "LDMFD   SP!, {R4,PC}\n"     // restore stack to PC instead of LR to return to caller
        );
}; 

void CreateTask_spytask() { 
        _CreateTask("SpyTask", 0x19, 0x2000, core_spytask, 0);

}; 

void CreateTask_PhySw() { //#fs 
        _CreateTask("PhySw", 0x18, 0x800, mykbd_task, 0);
}; 

void __attribute__((naked,noinline)) sub_FFC15BA4_my() { 
        asm volatile (
                "STMFD   SP!, {R4,LR}\n"
                "LDR     R4, =0x1CB8\n"
                "MOV     R0, #0\n"
                "MOV     R1, #4\n"
                "STR     R0, [R4,#0xC]\n"
                "BL      sub_FFC0BE20\n"
                "STR     R0, [R4,#4]\n"
                "MOV     R0, #0\n"
                "MOV     R1, #1\n"
                "BL      sub_FFC0BE44\n"
                "STR     R0, [R4,#8]\n"
                "BL      sub_FFC50EE8\n"
                "BL      sub_FFC4FCE0\n"
                "BL      sub_FFC4CE10_my\n" //-------------->
                "BL      sub_FFC511D0\n"
                "LDR     R0, [R4,#4]\n"
                "LDMFD   SP!, {R4,LR}\n"
                "MOV     R1, #0x1000\n"
                "B       sub_FFC18FAC\n"
        );
}; 

void __attribute__((naked,noinline)) sub_FFC4CE10_my() { 
        asm volatile (
                "STMFD   SP!, {R4,LR}\n"
                "LDR     R4, =0x5610\n"
                "LDR     R0, [R4,#8]\n"
                "CMP     R0, #0\n"
                "BNE     loc_FFC4CE7C\n"
          //    "BL      nullsub_8\n"
                "MOV     R1, #1\n"
                "MOV     R0, #0\n"
                "BL      sub_FFC0BE44\n"
                "STR     R0, [R4,#0x1C]\n"
                "MOV     R0, #0\n"
                "MOV     R1, #0\n"
                "BL      sub_FFC0BE20\n"
                "STR     R0, [R4,#0x20]\n"
                "BL      sub_FFC4D210\n"
                "BL      sub_FFC4D038\n"
                "MOV     R0, #0\n"
                "STR     R0, [R4,#0x18]\n"
                "ADD     R0, R4, #0x24\n"
                "MOV     R1, #0\n"
                "STR     R1, [R0],#4\n"
                "STR     R1, [R0]\n"
                "BL      sub_FFC4D528\n"
                "BL      sub_FFC5137C\n"
                "BL      sub_FFC4F650\n"
                "BL      sub_FFC4DE00_my\n" //------------------->
                "BL      sub_FFC4F134\n"     // taskcreate_MovieCtrlTask
"loc_FFC4CE7C:\n"
                "MOV     R0, #1\n"
                "STR     R0, [R4,#8]\n"
                "LDMFD   SP!, {R4,PC}\n"
        );
}; 

void __attribute__((naked,noinline)) sub_FFC4DE00_my() { 
        asm volatile (
                "STMFD   SP!, {R3-R5,LR}\n"
                "LDR     R2, =0x1CD44\n"
                "MOV     R0, #0\n"
                "MOV     R1, #0\n"
"loc_FFC4DE10:\n"
                "ADD     R3, R2, R0,LSL#4\n"
                "ADD     R0, R0, #1\n"
                "CMP     R0, #5\n"
                "STR     R1, [R3,#8]\n"
                "BCC     loc_FFC4DE10\n"
                "BL      sub_FFD0D958\n"
                "BL      sub_FFD0F710\n"
                "MOV     R1, #5\n"
                "MOV     R0, #0\n"
                "BL      sub_FFC0BDFC\n"
                "LDR     R4, =0x565C\n"
                "LDR     R1, =0x1001FF\n"
                "STR     R0, [R4,#0xC]\n"
                "MOV     R0, #0\n"
                "BL      sub_FFC0BE20\n"
                "STR     R0, [R4,#8]\n"
                "MOV     R3, #0\n"
                "STR     R3, [SP]\n"
                "LDR     R3, =task_CaptSeqTask_my\n"
                "LDR     R0, =0xFFC4DFD0\n"
                "MOV     R2, #0x1000\n"
                "MOV     R1, #0x17\n"
                "BL      sub_FFC0BDC8\n"
                "LDMFD   SP!, {R3-R5,PC}\n"
        ".ltorg\n"
        );
}; 


// -----------------
// Mutlipartition Boot Support
// -----------------


void __attribute__((naked,noinline)) sub_FFC1C888_my() { 
        asm volatile (
                "STMFD   SP!, {R4,LR}\n"
                "BL      sub_FFC5DE10\n"
                "BL      sub_FFC3AE04\n"
                "CMP     R0, #1\n"
                "BNE     loc_FFC1C8A8\n"
                "BL      sub_FFC60C44\n"
                "LDMFD   SP!, {R4,LR}\n"
                "B       sub_FFC3AE7C\n"
"loc_FFC1C8A8:\n"
                "BL      sub_FFC5FCC0\n"
                "LDR     R4, =0x1DD0\n"
                "LDR     R0, [R4,#4]\n"
                "CMP     R0, #0\n"
                "LDMNEFD SP!, {R4,PC}\n"
                "MOV     R1, #0\n"
                "LDR     R0, =sub_FFC1C454_my\n"  //-------------->
                "BL      sub_FFC5C140\n"
                "STR     R0, [R4,#4]\n"
                "LDMFD   SP!, {R4,PC}\n"
        );
}; 

void __attribute__((naked,noinline)) sub_FFC1C454_my() { 
        asm volatile (
                "STMFD   SP!, {R3-R11,LR}\n"
                "LDR     R6, =0x1DD0\n"
                "MOV     R5, R3\n"
                "LDR     R0, [R6,#0x14]\n"
                "MOV     R4, R1\n"
                "CMP     R0, #1\n"
                "BNE     loc_FFC1C478\n"
                "BL      sub_FFC5E6D4\n"
                "B       loc_FFC1C508\n"
"loc_FFC1C478:\n"
                "LDR     R11, =0x1167\n"
                "MOV     R7, #0\n"
                "SUB     R12, R11, #5\n"
                "CMP     R4, R12\n"
                "MOV     R9, #1\n"
                "BEQ     loc_FFC1C7B0\n"
                "BGT     loc_FFC1C5A8\n"
                "LDR     R0, =0x1061\n"
                "ORR     R12, R0, R0,ASR#11\n"
                "CMP     R4, R12\n"
                "ADD     R1, R0, #4\n"
                "BEQ     loc_FFC1C848\n"
                "BGT     loc_FFC1C534\n"
                "LDR     R12, =0x1056\n"
                "CMP     R4, R12\n"
                "BEQ     loc_FFC1C848\n"
                "BGT     loc_FFC1C510\n"
                "SUB     R12, R4, #0x800\n"
                "SUBS    R12, R12, #3\n"
                "BEQ     loc_FFC1C6C8\n"
                "SUB     R12, R4, #0x800\n"
                "SUBS    R12, R12, #0x158\n"
                "BEQ     loc_FFC1C864\n"
                "SUB     R12, R4, #0x900\n"
                "SUBS    R12, R12, #0xA3\n"
                "SUBNE   R12, R4, #0x900\n"
                "SUBNES  R12, R12, #0xA5\n"
                "BNE     loc_FFC1C880\n"
                "LDR     R0, [R6,#0xC]\n"
                "SUB     R12, R0, #0x8000\n"
                "SUBS    R12, R12, #2\n"
                "BEQ     loc_FFC1C508\n"
                "LDR     R0, =0x10A5\n"
                "BL      sub_FFC5CF00\n"
                "CMP     R0, #0\n"
                "BEQ     loc_FFC1C880\n"
"loc_FFC1C508:\n"
                "MOV     R0, #0\n"
                "LDMFD   SP!, {R3-R11,PC}\n"
"loc_FFC1C510:\n"
                "SUB     R12, R4, #0x1000\n"
                "SUBS    R12, R12, #0x5B\n"
                "SUBNE   R12, R4, #0x1000\n"
                "SUBNES  R12, R12, #0x5E\n"
                "CMPNE   R4, R0\n"
                "SUBNE   R12, R4, #0x1000\n"
                "SUBNES  R12, R12, #0x62\n"
                "BNE     loc_FFC1C880\n"
                "B       loc_FFC1C848\n"
"loc_FFC1C534:\n"
                "LDR     R12, =0x10AE\n"
                "CMP     R4, R12\n"
                "BEQ     loc_FFC1C7E4\n"
                "BGT     loc_FFC1C578\n"
                "CMP     R4, R1\n"
                "BEQ     loc_FFC1C848\n"
                "SUB     R12, R4, #0x1000\n"
                "LDR     R0, =0x10A3\n"
                "SUBS    R12, R12, #0xA9\n"
                "BEQ     loc_FFC1C7C0\n"
                "SUB     R12, R4, #0x1000\n"
                "SUBS    R12, R12, #0xAA\n"
                "BEQ     loc_FFC1C7D8\n"
                "SUB     R12, R4, #0x1000\n"
                "SUBS    R12, R12, #0xAD\n"
                "BNE     loc_FFC1C880\n"
                "B       loc_FFC1C7CC\n"
"loc_FFC1C578:\n"
                "SUB     R12, R4, #0x1000\n"
                "SUBS    R12, R12, #0xAF\n"
                "BEQ     loc_FFC1C7CC\n"
                "SUB     R12, R4, #0x1000\n"
                "SUBS    R12, R12, #0xB0\n"
                "BEQ     loc_FFC1C7E4\n"
                "SUB     R12, R4, #0x1000\n"
                "SUBS    R12, R12, #0xB2\n"
                "BNE     loc_FFC1C880\n"
                "LDR     R0, =0x1008\n"
                "MOV     R1, R5\n"
                "B       loc_FFC1C7A8\n"
"loc_FFC1C5A8:\n"
                "LDR     R10, =0x201B\n"
                "LDR     R0, =0x1DD0\n"
                "CMP     R4, R10\n"
                "LDR     R2, [R0,#0x10]!\n"
                "LDR     R1, [R0,#0x10]\n"
                "SUB     R8, R10, #0x17\n"
                "BEQ     loc_FFC1C784\n"
                "BGT     loc_FFC1C664\n"
                "LDR     R10, =0x116A\n"
                "CMP     R4, R10\n"
                "BEQ     loc_FFC1C770\n"
                "BGT     loc_FFC1C620\n"
                "SUB     R12, R4, #0x1100\n"
                "SUBS    R12, R12, #0x63\n"
                "MOVEQ   R1, #0\n"
                "MOVEQ   R0, #0x82\n"
                "BEQ     loc_FFC1C79C\n"
                "SUB     R12, R4, #0x1100\n"
                "SUBS    R12, R12, #0x65\n"
                "BEQ     loc_FFC1C794\n"
                "LDR     R5, =0x1168\n"
                "CMP     R4, R11\n"
                "CMPNE   R4, R5\n"
                "BNE     loc_FFC1C880\n"
                "STR     R9, [R6,#0x10]\n"
                "LDR     R6, =0x4508\n"
                "CMP     R1, #0\n"
                "BEQ     loc_FFC1C758\n"
                "BL      sub_FFC5E708\n"
                "B       loc_FFC1C764\n"
"loc_FFC1C620:\n"
                "SUB     R12, R4, #0x2000\n"
                "SUBS    R12, R12, #2\n"
                "BEQ     loc_FFC1C814\n"
                "CMP     R4, R8\n"
                "MOV     R0, R8\n"
                "BEQ     loc_FFC1C820\n"
                "SUB     R12, R4, #0x2000\n"
                "SUBS    R12, R12, #5\n"
                "BEQ     loc_FFC1C814\n"
                "SUB     R12, R4, #0x2000\n"
                "SUBS    R12, R12, #0x19\n"
                "BNE     loc_FFC1C880\n"
                "CMP     R1, #0\n"
                "BEQ     loc_FFC1C508\n"
                "CMP     R2, #0\n"
                "BNE     loc_FFC1C508\n"
                "B       loc_FFC1C85C\n"
"loc_FFC1C664:\n"
                "LDR     R12, =0x3111\n"
                "CMP     R4, R12\n"
                "BEQ     loc_FFC1C7E8\n"
                "BGT     loc_FFC1C6A4\n"
                "SUB     R12, R4, #0x2000\n"
                "SUBS    R12, R12, #0x1D\n"
                "BEQ     loc_FFC1C814\n"
                "LDR     R0, =0x2027\n"
                "CMP     R4, R0\n"
                "BEQ     loc_FFC1C7F0\n"
                "SUB     R12, R4, #0x3000\n"
                "SUBS    R12, R12, #6\n"
                "SUBNE   R12, R4, #0x3000\n"
                "SUBNES  R12, R12, #0x110\n"
                "BNE     loc_FFC1C880\n"
                "B       loc_FFC1C7E8\n"
"loc_FFC1C6A4:\n"
                "CMP     R4, #0x3140\n"
                "BEQ     loc_FFC1C83C\n"
                "SUB     R12, R4, #0x3200\n"
                "SUBS    R12, R12, #1\n"
                "BEQ     loc_FFC1C880\n"
                "SUB     R12, R4, #0x3200\n"
                "SUBS    R12, R12, #2\n"
                "BNE     loc_FFC1C880\n"
                "B       loc_FFC1C7E8\n"
"loc_FFC1C6C8:\n"
                "MOV     R4, #1\n"
                "MOV     R0, #2\n"
                "BL      sub_FFC5DEA4\n"
                "CMP     R0, #1\n"
                "MOVEQ   R4, #2\n"
                "MOV     R0, R4\n"
                "BL      sub_FFC123A8\n"
                "CMP     R0, #0\n"
                "STRNE   R9, [R6,#0x14]\n"
                "BNE     loc_FFC1C724\n"
                "BL      sub_FFC63B60\n"
                "BL      sub_FFC6157C\n"
                "BL      sub_FFC6223C\n"
                "BL      sub_FFC626B4\n"
                "BL      sub_FFC60EB4\n"
                "BL      sub_FFC62C70\n"
                "CMP     R0, #0\n"
                "BEQ     loc_FFC1C72C\n"
                "BL      sub_FFC1C178\n"
                "BL      sub_FFC62BD4\n"
                "MOV     R1, R0\n"
                "MOV     R0, R11\n"
                "BL      sub_FFC5C93C\n"
"loc_FFC1C724:\n"
                "MOV     R0, R7\n"
                "LDMFD   SP!, {R3-R11,PC}\n"
"loc_FFC1C72C:\n"
                "BL      sub_FFC161B4\n"
                "CMP     R0, #1\n"
                "LDRNE   R0, =0x310B\n"
                "LDREQ   R0, =0x310C\n"
                "MOV     R1, #0\n"
                "BL      sub_FFC5C93C\n"
                "BL      sub_FFC60834_my\n" //--------------->
                "B       loc_FFC1C724\n"
"loc_FFC1C74C:\n"
                "MOV     R0, R6\n"
                "BL      sub_FFC4B774\n"
                "B       loc_FFC1C508\n"
"loc_FFC1C758:\n"
                "BL      sub_FFC61808\n"
                "BL      sub_FFC6176C\n"
                "BL      sub_FFC15ECC\n"
"loc_FFC1C764:\n"
                "CMP     R4, R5\n"
                "BNE     loc_FFC1C508\n"
                "B       loc_FFC1C74C\n"
"loc_FFC1C770:\n"
                "MOV     R0, #1\n"
                "BL      sub_FFC5E834\n"
                "LDR     R0, =0x1005\n"
                "MOV     R1, R10\n"
                "B       loc_FFC1C7A8\n"
"loc_FFC1C784:\n"
                "CMP     R2, #1\n"
                "BNE     loc_FFC1C7E8\n"
                "BL      sub_FFC5E708\n"
                "B       loc_FFC1C508\n"
"loc_FFC1C794:\n"
                "MOV     R1, #0\n"
                "MOV     R0, #0x83\n"
"loc_FFC1C79C:\n"
                "BL      sub_FFC6277C\n"
                "B       loc_FFC1C508\n"
"loc_FFC1C7A4:\n"
                "MOV     R0, R4\n"
"loc_FFC1C7A8:\n"
                "BL      sub_FFC5F9E8_my\n"  //------------------->
                "LDMFD   SP!, {R3-R11,PC}\n"
"loc_FFC1C7B0:\n"
                "BL      sub_FFC65D1C\n"
                "CMP     R0, #0\n"
                "BLNE    sub_FFC64AB0\n"
                "B       loc_FFC1C7E8\n"
"loc_FFC1C7C0:\n"
                "BL      sub_FFC5CF00\n"
                "CMP     R0, #0\n"
                "BNE     loc_FFC1C508\n"
"loc_FFC1C7CC:\n"
                "MOV     R0, R4\n"
                "BL      sub_FFC1C244\n"
                "LDMFD   SP!, {R3-R11,PC}\n"
"loc_FFC1C7D8:\n"
                "BL      sub_FFC5CF00\n"
                "CMP     R0, #0\n"
                "BNE     loc_FFC1C508\n"
"loc_FFC1C7E4:\n"
                "BL      sub_FFC1CCC0\n"
"loc_FFC1C7E8:\n"
                "MOV     R1, R5\n"
                "B       loc_FFC1C7A4\n"
"loc_FFC1C7F0:\n"
                "MOV     R1, #0\n"
                "BL      sub_FFC5F9E8_my\n"   //-------------------->
                "MOV     R1, #0\n"
                "MOV     R0, R10\n"
                "BL      sub_FFC5F9E8_my\n"   //-------------------->
                "MOV     R1, #0\n"
                "MOV     R0, R8\n"
"loc_FFC1C80C:\n"
                "BL      sub_FFC5F9E8_my\n"   //-------------------->
                "B       loc_FFC1C508\n"
"loc_FFC1C814:\n"
                "STR     R7, [R6,#0x20]\n"
                "BL      sub_FFC1CA5C\n"
                "B       loc_FFC1C7E8\n"
"loc_FFC1C820:\n"
                "STR     R7, [R6,#0x20]\n"
                "BL      sub_FFC1CA5C\n"
                "LDR     R0, [R6,#0x10]\n"
                "CMP     R0, #1\n"
                "BNE     loc_FFC1C7E8\n"
                "BL      sub_FFC5E74C\n"
                "B       loc_FFC1C508\n"
"loc_FFC1C83C:\n"
                "CMP     R1, #0\n"
                "BLEQ    sub_FFC1CA5C\n"
                "B       loc_FFC1C508\n"
"loc_FFC1C848:\n"
                "CMP     R4, R0\n"
                "BEQ     loc_FFC1C85C\n"
                "CMP     R4, R1\n"
                "MVNNE   R0, #0\n"
                "MOVEQ   R0, R1\n"
"loc_FFC1C85C:\n"
                "MOV     R1, #0\n"
                "B       loc_FFC1C80C\n"
"loc_FFC1C864:\n"
                "TST     R5, #0x80000000\n"
                "MOVNE   R0, #1\n"
                "LDMNEFD SP!, {R3-R11,PC}\n"
                "BL      sub_FFC670A0\n"
                "CMP     R0, #0\n"
                "BLEQ    sub_FFC18AF4\n"
                "B       loc_FFC1C508\n"
"loc_FFC1C880:\n"
                "MOV     R1, #0\n"
                "B       loc_FFC1C7A4\n"
        );
}; 

void __attribute__((naked,noinline)) sub_FFC60834_my() { 
        asm volatile (
                "STMFD   SP!, {R4,LR}\n"
                "BL      sub_FFC1CA10\n"
                "CMP     R0, #0\n"
                "LDRNE   R0, =0x1061\n"
                "BNE     loc_FFC6085C\n"
                "BL      sub_FFC1C9F8\n"
                "CMP     R0, #0\n"
                "LDRNE   R0, =0x105B\n"
                "BNE     loc_FFC6085C\n"
                "BL      sub_FFC1CA28\n"
"loc_FFC6085C:\n"
                "MOV     R4, R0\n"
                "BL      sub_FFC6099C\n"
                "MOV     R0, R4\n"
                "BL      sub_FFC60A3C\n"
                "BL      sub_FFC1CA28\n"
                "MOV     R4, R0\n"
                "LDR     R0, =0x5A8C\n"
                "LDR     R0, [R0]\n"
                "TST     R0, #1\n"
                "TSTEQ   R0, #0x10\n"
                "BEQ     loc_FFC60894\n"
"loc_FFC60888:\n"
                "MOV     R1, R4\n"
                "MOV     R0, #2\n"
                "B       loc_FFC608D8\n"
"loc_FFC60894:\n"
                "TST     R0, #0x40\n"
                "BEQ     loc_FFC608A8\n"
"loc_FFC6089C:\n"
                "MOV     R1, R4\n"
                "MOV     R0, #1\n"
                "B       loc_FFC608D8\n"
"loc_FFC608A8:\n"
                "TST     R0, #0x20\n"
                "BEQ     loc_FFC608C4\n"
                "TST     R0, #0x4000\n"
                "BNE     loc_FFC608C4\n"
"loc_FFC608B8:\n"
                "MOV     R1, R4\n"
                "MOV     R0, #0\n"
                "B       loc_FFC608D8\n"
"loc_FFC608C4:\n"
                "LDR     R1, =0x102\n"
                "BICS    R1, R1, R0\n"
                "BNE     loc_FFC608E0\n"
                "MOV     R1, R4\n"
                "MOV     R0, #6\n"
"loc_FFC608D8:\n"
                "LDMFD   SP!, {R4,LR}\n"
                "B       sub_FFC607D0_my\n"   //-------------->
"loc_FFC608E0:\n"
                "TST     R0, #0x100\n"
                "BNE     loc_FFC60888\n"
                "TST     R0, #0x400\n"
                "BNE     loc_FFC6089C\n"
                "TST     R0, #0x200\n"
                "TSTEQ   R0, #2\n"
                "BNE     loc_FFC608B8\n"
                "TST     R0, #0x40000\n"
                "BEQ     loc_FFC60888\n"
                "TST     R0, #0x200000\n"
                "MOVEQ   R1, R4\n"
                "MOVEQ   R0, #1\n"
                "BLEQ    sub_FFC607D0_my\n"   //--------------->
                "B       loc_FFC60888\n"
        );
}; 

void __attribute__((naked,noinline)) sub_FFC607D0_my() { 
        asm volatile (
                "STMFD   SP!, {R4-R6,LR}\n"
                "MOVS    R4, R0\n"
                "MOV     R0, #1\n"
                "MOV     R5, R1\n"
                "BNE     loc_FFC60810\n"
                "MOV     R1, #0\n"
                "MOV     R0, #0\n"
                "BL      sub_FFC5DE34\n"
                "BL      sub_FFC1CA28\n"
                "SUB     R12, R0, #0x1000\n"
                "SUBS    R12, R12, #0x5B\n"
                "BNE     loc_FFC60808\n"
"loc_FFC60800:\n"
                "BL      sub_FFC60724\n"
                "B       loc_FFC60818\n"
"loc_FFC60808:\n"
                "BL      sub_FFC60764\n"
                "B       loc_FFC60818\n"
"loc_FFC60810:\n"
                "CMP     R4, #5\n"
                "BEQ     loc_FFC60800\n"
"loc_FFC60818:\n"
                "CMP     R0, #0\n"
                "LDREQ   R5, =0x1162\n"
                "MOVEQ   R4, #2\n"
                "MOV     R0, R4\n"
                "MOV     R1, R5\n"
                "LDMFD   SP!, {R4-R6,LR}\n"
                "B       sub_FFC5F9E8_my\n"   //------------------->
        );
}; 

void __attribute__((naked,noinline)) sub_FFC5F9E8_my() { 
        asm volatile (
                "STMFD   SP!, {R4-R8,LR}\n"
                "MOV     R7, R1\n"
                "MOV     R6, R0\n"
                "BL      sub_FFC62C70\n"
                "CMP     R0, #0\n"
                "BNE     loc_FFC5FCB8\n"
                "MOV     R1, R7\n"
                "MOV     R0, R6\n"
                "BL      sub_FFC5EA0C\n"
                "LDR     R4, =0x59D0\n"
                "MOV     R5, #0\n"
                "LDR     R0, [R4,#0x10]\n"
                "CMP     R0, #0x16\n"
                "ADDLS   PC, PC, R0,LSL#2\n"
                "B       loc_FFC5FCB8\n"
"loc_FFC5FA24:\n"
                "B       loc_FFC5FA80\n"
"loc_FFC5FA28:\n"
                "B       loc_FFC5FA9C\n"
"loc_FFC5FA2C:\n"
                "B       loc_FFC5FAD0\n"
"loc_FFC5FA30:\n"
                "B       loc_FFC5FB80\n"
"loc_FFC5FA34:\n"
                "B       loc_FFC5FB90\n"
"loc_FFC5FA38:\n"
                "B       loc_FFC5FCB8\n"
"loc_FFC5FA3C:\n"
                "B       loc_FFC5FCB8\n"
"loc_FFC5FA40:\n"
                "B       loc_FFC5FC14\n"
"loc_FFC5FA44:\n"
                "B       loc_FFC5FA90\n"
"loc_FFC5FA48:\n"
                "B       loc_FFC5FCB8\n"
"loc_FFC5FA4C:\n"
                "B       loc_FFC5FC14\n"
"loc_FFC5FA50:\n"
                "B       loc_FFC5FAC4\n"
"loc_FFC5FA54:\n"
                "B       loc_FFC5FCB8\n"
"loc_FFC5FA58:\n"
                "B       loc_FFC5FCB8\n"
"loc_FFC5FA5C:\n"
                "B       loc_FFC5FADC\n"
"loc_FFC5FA60:\n"
                "B       loc_FFC5FAE8\n"
"loc_FFC5FA64:\n"
                "B       loc_FFC5FB58\n"
"loc_FFC5FA68:\n"
                "B       loc_FFC5FAA8\n"
"loc_FFC5FA6C:\n"
                "B       loc_FFC5FCA0\n"
"loc_FFC5FA70:\n"
                "B       loc_FFC5FC20\n"
"loc_FFC5FA74:\n"
                "B       loc_FFC5FC50\n"
"loc_FFC5FA78:\n"
                "B       loc_FFC5FC50\n"
"loc_FFC5FA7C:\n"
                "B       loc_FFC5FB9C\n"
"loc_FFC5FA80:\n"
                "MOV     R1, R7\n"
                "MOV     R0, R6\n"
                "LDMFD   SP!, {R4-R8,LR}\n"
                "B       sub_FFC5F398_my\n"   //------------------>
"loc_FFC5FA90:\n"
                "MOV     R0, R6\n"
                "LDMFD   SP!, {R4-R8,LR}\n"
                "B       sub_FFC603E4\n"
"loc_FFC5FA9C:\n"
                "MOV     R0, R6\n"
                "LDMFD   SP!, {R4-R8,LR}\n"
                "B       sub_FFC5F034\n"
"loc_FFC5FAA8:\n"
                "SUB     R12, R6, #0x3000\n"
                "SUBS    R12, R12, #6\n"
                "BNE     loc_FFC5FCB8\n"
                "MOV     R0, #0\n"
                "BL      sub_FFC1C010\n"
                "BL      sub_FFC602DC\n"
                "B       loc_FFC5FCB0\n"
"loc_FFC5FAC4:\n"
                "MOV     R0, R6\n"
                "LDMFD   SP!, {R4-R8,LR}\n"
                "B       sub_FFC60314\n"
"loc_FFC5FAD0:\n"
                "MOV     R0, R6\n"
                "LDMFD   SP!, {R4-R8,LR}\n"
                "B       sub_FFC5F1C8\n"
"loc_FFC5FADC:\n"
                "MOV     R0, R6\n"
                "LDMFD   SP!, {R4-R8,LR}\n"
                "B       sub_FFC5F788\n"
"loc_FFC5FAE8:\n"
                "SUB     R12, R6, #0x3200\n"
                "SUBS    R12, R12, #2\n"
                "BNE     loc_FFC5FCB8\n"
                "MOV     R0, #3\n"
                "BL      sub_FFC5E918\n"
                "MOV     R0, #8\n"
                "BL      sub_FFC1BF78\n"
                "MOV     R1, #0\n"
                "MOV     R0, #0x19\n"
                "BL      sub_FFC2D690\n"
                "BL      sub_FFC61808\n"
                "BL      sub_FFC61AA4\n"
                "BL      sub_FFC60F24\n"
                "B       loc_FFC5FCB0\n"
"loc_FFC5FB58:\n"
                "SUB     R12, R6, #0x3300\n"
                "SUBS    R12, R12, #1\n"
                "BNE     loc_FFC5FCB8\n"
                "LDR     R0, =0x4010\n"
                "BL      sub_FFC1BF78\n"
                "BL      sub_FFD5F4D8\n"
                "BL      sub_FFC60F24\n"
                "MOV     R0, #4\n"
                "BL      sub_FFC5E918\n"
                "B       loc_FFC5FCB0\n"
"loc_FFC5FB80:\n"
                "MOV     R1, R7\n"
                "MOV     R0, R6\n"
                "LDMFD   SP!, {R4-R8,LR}\n"
                "B       sub_FFC5F8E4\n"
"loc_FFC5FB90:\n"
                "MOV     R0, R6\n"
                "LDMFD   SP!, {R4-R8,LR}\n"
                "B       sub_FFC60528\n"
"loc_FFC5FB9C:\n"
                "LDR     R7, =0x1182\n"
                "MOV     R0, R6\n"
                "CMP     R0, R7\n"
                "MOV     R6, #1\n"
                "STREQ   R6, [R4,#0xA8]\n"
                "BEQ     loc_FFC5FCB0\n"
                "SUB     R12, R0, #0x1000\n"
                "SUBS    R12, R12, #0x1AC\n"
                "BEQ     loc_FFC5FBFC\n"
                "SUB     R12, R0, #0x3000\n"
                "SUBS    R12, R12, #0x224\n"
                "BNE     loc_FFC5FCB8\n"
                "MOV     R0, #8\n"
                "BL      sub_FFC1BF78\n"
                "MOV     R0, #3\n"
                "BL      sub_FFC5E918\n"
                "STR     R5, [R4,#0xAC]\n"
                "LDR     R0, [R4,#0xA8]\n"
                "CMP     R0, #0\n"
                "MOVNE   R1, #0\n"
                "MOVNE   R0, R7\n"
                "STRNE   R5, [R4,#0xA8]\n"
                "BLNE    sub_FFC5F8E4\n"
                "B       loc_FFC5FCB0\n"
"loc_FFC5FBFC:\n"
                "LDR     R0, [R4,#0xAC]\n"
                "CMP     R0, #0\n"
                "BNE     loc_FFC5FCB0\n"
                "BL      sub_FFD5D458\n"
                "STR     R6, [R4,#0xAC]\n"
                "B       loc_FFC5FCB0\n"
"loc_FFC5FC14:\n"
                "MOV     R0, R6\n"
                "LDMFD   SP!, {R4-R8,LR}\n"
                "B       sub_FFC5F6A8_my\n"  //---------> movie
"loc_FFC5FC20:\n"
                "LDR     R12, =0x10B0\n"
                "CMP     R6, R12\n"
                "BEQ     loc_FFC5FC4C\n"
                "BGT     loc_FFC5FC58\n"
                "CMP     R6, #4\n"
                "BEQ     loc_FFC5FC80\n"
                "SUB     R12, R6, #0x1000\n"
                "SUBS    R12, R12, #0xAA\n"
                "SUBNE   R12, R6, #0x1000\n"
                "SUBNES  R12, R12, #0xAE\n"
                "BNE     loc_FFC5FCB8\n"
"loc_FFC5FC4C:\n"
                "BL      sub_FFC5E5F0\n"
"loc_FFC5FC50:\n"
                "MOV     R0, R5\n"
                "LDMFD   SP!, {R4-R8,PC}\n"
"loc_FFC5FC58:\n"
                "SUB     R12, R6, #0x2000\n"
                "SUBS    R12, R12, #4\n"
                "BEQ     loc_FFC5FC98\n"
                "SUB     R12, R6, #0x5000\n"
                "SUBS    R12, R12, #1\n"
                "SUBNE   R12, R6, #0x5000\n"
                "SUBNES  R12, R12, #6\n"
                "BNE     loc_FFC5FCB8\n"
                "BL      sub_FFC5EFA0\n"
                "B       loc_FFC5FCB0\n"
"loc_FFC5FC80:\n"
                "LDR     R0, [R4,#0x2C]\n"
                "CMP     R0, #0\n"
                "BNE     loc_FFC5FC98\n"
                "BL      sub_FFC5FE78\n"
                "BL      sub_FFC15E84\n"
                "B       loc_FFC5FCB0\n"
"loc_FFC5FC98:\n"
                "BL      sub_FFC5E62C\n"
                "B       loc_FFC5FCB0\n"
"loc_FFC5FCA0:\n"
                "SUB     R12, R6, #0x3000\n"
                "SUBS    R12, R12, #0x130\n"
                "BNE     loc_FFC5FCB8\n"
                "BL      sub_FFC5E6D4\n"
"loc_FFC5FCB0:\n"
                "MOV     R0, #0\n"
                "LDMFD   SP!, {R4-R8,PC}\n"
"loc_FFC5FCB8:\n"
                "MOV     R0, #1\n"
                "LDMFD   SP!, {R4-R8,PC}\n"
        );
}; 

void __attribute__((naked,noinline)) sub_FFC5F398_my() { 
        asm volatile (
                "STMFD   SP!, {R4-R8,LR}\n"
                "LDR     R7, =0x8002\n"
                "LDR     R4, =0x59D0\n"
                "CMP     R0, #2\n"
                "MOV     R6, R1\n"
                "MOV     R5, #1\n"
                "BEQ     loc_FFC5F434\n"
                "BGT     loc_FFC5F41C\n"
                "CMP     R0, #0\n"
                "BEQ     loc_FFC5F460\n"
                "CMP     R0, #1\n"
                "BNE     loc_FFC5F504\n"
                "MOV     R0, #8\n"
                "BL      sub_FFC5E918\n"
                "BL      sub_FFC60B74\n"
                "BL      sub_FFC6165C\n"
                "LDR     R5, =0x4004\n"
                "MOV     R0, #0\n"
                "MOV     R1, R5\n"
                "BL      sub_FFC6277C\n"
                "LDR     R1, =0xFFC5F664\n"
                "MOV     R0, #0x20\n"
                "BL      sub_FFC59164\n"
                "BL      sub_FFC60B38_my\n"  //---------------->
                "BL      sub_FFC60C44\n"
                "BL      sub_FFC1C978\n"
                "MOV     R0, R5\n"
                "BL      sub_FFC1BF78\n"
                "LDR     R0, [R4,#0x68]\n"
                "CMP     R0, #0\n"
                "BNE     loc_FFC5F4E4\n"
                "BL      sub_FFC1C154\n"
                "B       loc_FFC5F4E8\n"
"loc_FFC5F41C:\n"
                "CMP     R0, #6\n"
                "STREQ   R5, [R4,#0x28]\n"
                "BEQ     loc_FFC5F4F8\n"
                "SUB     R12, R0, #0x2000\n"
                "SUBS    R12, R12, #4\n"
                "BNE     loc_FFC5F504\n"
"loc_FFC5F434:\n"
                "SUB     R12, R6, #0x1100\n"
                "SUBS    R12, R12, #0x62\n"
                "BNE     loc_FFC5F450\n"
                "MOV     R1, R7\n"
                "MOV     R0, #0\n"
                "BL      sub_FFC6277C\n"
                "STR     R5, [R4,#0x60]\n"
"loc_FFC5F450:\n"
                "BL      sub_FFC61808\n"
                "BL      sub_FFC61AA4\n"
                "BL      sub_FFC5EF40\n"
                "B       loc_FFC5F4FC\n"
"loc_FFC5F460:\n"
                "MOV     R0, #7\n"
                "BL      sub_FFC5E918\n"
                "MOV     R0, R7\n"
                "BL      sub_FFC1BF78\n"
                "BL      sub_FFC60B74\n"
                "BL      sub_FFC6165C\n"
                "MOV     R1, R7\n"
                "MOV     R0, #0\n"
                "BL      sub_FFC6277C\n"
                "LDR     R1, =0xFFC5F670\n"
                "MOV     R0, #0x20\n"
                "STR     R6, [R4,#0x18]\n"
                "BL      sub_FFC59164\n"
                "LDR     R1, =0xFFC5F67C\n"
                "MOV     R0, #0x20\n"
                "BL      sub_FFC59164\n"
                "STR     R5, [R4,#0x28]\n"
                "BL      sub_FFC1C0E4\n"
                "BL      sub_FFC1C038\n"
                "LDR     R0, [R4,#0x1C]\n"
                "LDR     R1, [R4,#0x20]\n"
                "ORRS    R0, R0, R1\n"
                "BLNE    sub_FFC5FE54\n"
                "LDR     R0, [R4,#0x68]\n"
                "CMP     R0, #0\n"
                "BNE     loc_FFC5F4D0\n"
                "BL      sub_FFC1C154\n"
                "B       loc_FFC5F4D8\n"
"loc_FFC5F4D0:\n"
                "BL      sub_FFC15C04\n"
                "BL      sub_FFC1C9B0\n"
"loc_FFC5F4D8:\n"
                "BL      sub_FFC60B38_my\n" //--------------->
                "BL      sub_FFC60BB0\n"
                "B       loc_FFC5F4FC\n"
"loc_FFC5F4E4:\n"
                "BL      sub_FFC15C04\n"
"loc_FFC5F4E8:\n"
                "BL      sub_FFC60BE0\n"
                "LDR     R0, [R4,#0x30]\n"
                "CMP     R0, #0\n"
                "BEQ     loc_FFC5F4FC\n"
"loc_FFC5F4F8:\n"
                "BL      sub_FFC5FE9C\n"
"loc_FFC5F4FC:\n"
                "MOV     R0, #0\n"
                "LDMFD   SP!, {R4-R8,PC}\n"
"loc_FFC5F504:\n"
                "MOV     R0, #1\n"
                "LDMFD   SP!, {R4-R8,PC}\n"
        );
};

void __attribute__((naked,noinline)) sub_FFC60B38_my() {
        asm volatile (
                "LDR     R0, =0x5A98\n"
                "STMFD   SP!, {R3,LR}\n"
                "LDR     R1, [R0,#4]\n"
                "CMP     R1, #0\n"
                "BNE     loc_FFC60B70\n"
                "MOV     R1, #1\n"
                "STR     R1, [R0,#4]\n"
                "MOV     R3, #0\n"
                "STR     R3, [SP]\n"
                "LDR     R3, =task_InitFileModules_my\n"  //-------------------->
                "MOV     R1, #0x19\n"
                "LDR     R0, =0xFFC60C9C\n" // "InitFileModules"
                "MOV     R2, #0x1000\n"
                "BL      sub_FFC0BAF0\n"
"loc_FFC60B70:\n"
                "LDMFD   SP!, {R12,PC}\n"
        );
}; 

void __attribute__((naked,noinline)) task_InitFileModules_my() { 
        asm volatile (
                "STMFD   SP!, {R4-R6,LR}\n"
                "BL      sub_FFC5B75C\n"
                "LDR     R5, =0x5006\n"
                "MOVS    R4, R0\n"
                "MOVNE   R1, #0\n"
                "MOVNE   R0, R5\n"
                "BLNE    sub_FFC5C93C\n"
                "BL      sub_FFC5B788_my\n"    //---------------->

                "BL      core_spytask_can_start\n"      // CHDK: Set "it's-save-to-start"-Flag for spytask

                "CMP     R4, #0\n"
                "MOVEQ   R0, R5\n"
                "LDMEQFD SP!, {R4-R6,LR}\n"
                "MOVEQ   R1, #0\n"
                "BEQ     sub_FFC5C93C\n"
                "LDMFD   SP!, {R4-R6,PC}\n"
        );
}; 

void __attribute__((naked,noinline)) sub_FFC5B788_my() { 
        asm volatile (
                "STMFD   SP!, {R4,LR}\n"
                "BL      sub_FFC3FC8C_my\n" //------------------->
                "LDR     R4, =0x58B0\n"
                "LDR     R0, [R4,#4]\n"
                "CMP     R0, #0\n"
                "BNE     loc_FFC5B7B8\n"
                "BL      sub_FFC69168\n"
                "BL      sub_FFCE01E8\n"
                "BL      sub_FFC69168\n"
                "BL      sub_FFCE9CC8\n"
                "BL      sub_FFC69178\n"
                "BL      sub_FFCE0288\n"
"loc_FFC5B7B8:\n"
                "MOV     R0, #1\n"
                "STR     R0, [R4]\n"
                "LDMFD   SP!, {R4,PC}\n"
        );
}; 

void __attribute__((naked,noinline)) sub_FFC3FC8C_my() { 
        asm volatile (
                "STMFD   SP!, {R4-R6,LR}\n"
                "MOV     R6, #0\n"
                "MOV     R0, R6\n"
                "BL      sub_FFC3F85C\n"
                "LDR     R4, =0x1526C\n"
                "MOV     R5, #0\n"
                "LDR     R0, [R4,#0x38]\n"
                "BL      sub_FFC40224\n"
                "CMP     R0, #0\n"
                "LDREQ   R0, =0x2B7C\n"
                "STREQ   R5, [R0,#0xC]\n"
                "STREQ   R5, [R0,#0x10]\n"
                "STREQ   R5, [R0,#0x14]\n"
                "MOV     R0, R6\n"
                "BL      sub_FFC3F89C\n"
                "MOV     R0, R6\n"
                "BL      sub_FFC3FAC8_my\n"  //------------------>
                "MOV     R5, R0\n"
                "MOV     R0, R6\n"
                "BL      sub_FFC3FB34\n"
                "LDR     R1, [R4,#0x3C]\n"
                "AND     R2, R5, R0\n"
                "CMP     R1, #0\n"
                "MOV     R0, #0\n"
                "MOVEQ   R0, #0x80000001\n"
                "BEQ     loc_FFC3FD20\n"
                "LDR     R3, [R4,#0x2C]\n"
                "CMP     R3, #2\n"
                "MOVEQ   R0, #4\n"
                "CMP     R1, #5\n"
                "ORRNE   R0, R0, #1\n"
                "BICEQ   R0, R0, #1\n"
                "CMP     R2, #0\n"
                "BICEQ   R0, R0, #2\n"
                "ORREQ   R0, R0, #0x80000000\n"
                "BICNE   R0, R0, #0x80000000\n"
                "ORRNE   R0, R0, #2\n"
"loc_FFC3FD20:\n"
                "STR     R0, [R4,#0x40]\n"
                "LDMFD   SP!, {R4-R6,PC}\n"
        );
}; 

void __attribute__((naked,noinline)) sub_FFC3FAC8_my() { 
        asm volatile (
                "STMFD   SP!, {R4-R6,LR}\n"
                "LDR     R5, =0x2B7C\n"
                "MOV     R6, R0\n"
                "LDR     R0, [R5,#0x10]\n"
                "CMP     R0, #0\n"
                "MOVNE   R0, #1\n"
                "LDMNEFD SP!, {R4-R6,PC}\n"
                "MOV     R0, #0x17\n"
                "MUL     R1, R0, R6\n"
                "LDR     R0, =0x1526C\n"
                "ADD     R4, R0, R1,LSL#2\n"
                "LDR     R0, [R4,#0x38]\n"
                "MOV     R1, R6\n"
                "BL      sub_FFC3F960_my\n" //------------------->
                "CMP     R0, #0\n"
                "LDMEQFD SP!, {R4-R6,PC}\n"
                "LDR     R0, [R4,#0x38]\n"
                "MOV     R1, R6\n"
                "BL      sub_FFC4033C\n"
                "CMP     R0, #0\n"
                "LDMEQFD SP!, {R4-R6,PC}\n"
                "MOV     R0, R6\n"
                "BL      sub_FFC3F47C\n"
                "CMP     R0, #0\n"
                "MOVNE   R1, #1\n"
                "STRNE   R1, [R5,#0x10]\n"
                "LDMFD   SP!, {R4-R6,PC}\n"
        );
};

void __attribute__((naked,noinline)) sub_FFC3F960_my() { // Partition table parse takes place here. 
        asm volatile (
                "STMFD   SP!, {R4-R8,LR}\n"
                "MOV     R8, R0\n"
                "MOV     R0, #0x17\n"
                "MUL     R1, R0, R1\n"
                "LDR     R0, =0x1526C\n"
                "MOV     R6, #0\n"
                "ADD     R7, R0, R1,LSL#2\n"
                "LDR     R0, [R7,#0x3C]\n"
                "MOV     R5, #0\n"
                "CMP     R0, #6\n"
                "ADDLS   PC, PC, R0,LSL#2\n"
                "B       loc_FFC3FAAC\n"
"loc_FFC3F990:\n"
                "B       loc_FFC3F9C4\n"
"loc_FFC3F994:\n"
                "B       loc_FFC3F9AC\n"
"loc_FFC3F998:\n"
                "B       loc_FFC3F9AC\n"
"loc_FFC3F99C:\n"
                "B       loc_FFC3F9AC\n"
"loc_FFC3F9A0:\n"
                "B       loc_FFC3F9AC\n"
"loc_FFC3F9A4:\n"
                "B       loc_FFC3FAA4\n"
"loc_FFC3F9A8:\n"
                "B       loc_FFC3F9AC\n"
"loc_FFC3F9AC:\n"
                "MOV     R2, #0\n"
                "MOV     R1, #0x200\n"
                "MOV     R0, #3\n"
                "BL      sub_FFC57C34\n"
                "MOVS    R4, R0\n"
                "BNE     loc_FFC3F9CC\n"
"loc_FFC3F9C4:\n"
                "MOV     R0, #0\n"
                "LDMFD   SP!, {R4-R8,PC}\n"
"loc_FFC3F9CC:\n"
                "LDR     R12, [R7,#0x4C]\n"
                "MOV     R3, R4\n"
                "MOV     R2, #1\n"
                "MOV     R1, #0\n"
                "MOV     R0, R8\n"

                //"BLX     R12\n"       // !! Workaround !!
                "MOV     LR, PC\n"      // gcc won't compile "BLX R12" nor "BL R12".
                "MOV     PC, R12\n"     // workaround: make your own "BL" and hope we don't need the change to thumb-mode

                "CMP     R0, #1\n"
                "BNE     loc_FFC3F9F8\n"
                "MOV     R0, #3\n"
                "BL      sub_FFC57D74\n"
                "B       loc_FFC3F9C4\n"

"loc_FFC3F9F8:\n"
                "MOV     R0, R8\n"
                "BL      sub_FFCF8D78\n"     // Add FAT32 autodetect-code after this line

                // Start of DataGhost's FAT32 autodetection code
                // Policy: If there is a partition which has type W95 FAT32, use the first one of those for image storage
                // According to the code below, we can use R1, R2, R3 and R12.
                // LR wasn't really used anywhere but for storing a part of the partition signature. This is the only thing
                // that won't work with an offset, but since we can load from LR+offset into LR, we can use this to do that :)
                "MOV     R12, R4\n"                    // Copy the MBR start address so we have something to work with
                "MOV     LR, R4\n"                     // Save old offset for MBR signature
                "MOV     R1, #1\n"                     // Note the current partition number
                "B       dg_sd_fat32_enter\n"          // We actually need to check the first partition as well, no increments yet!
           "dg_sd_fat32:\n"
                "CMP     R1, #4\n"                     // Did we already see the 4th partition?
                "BEQ     dg_sd_fat32_end\n"            // Yes, break. We didn't find anything, so don't change anything.
                "ADD     R12, R12, #0x10\n"            // Second partition
                "ADD     R1, R1, #1\n"                 // Second partition for the loop
           "dg_sd_fat32_enter:\n"
                "LDRB    R2, [R12, #0x1BE]\n"          // Partition status
                "LDRB    R3, [R12, #0x1C2]\n"          // Partition type (FAT32 = 0xB)
                "CMP     R3, #0xB\n"                   // Is this a FAT32 partition?
                "CMPNE   R3, #0xC\n"                   // Not 0xB, is it 0xC (FAT32 LBA) then?
                "BNE     dg_sd_fat32\n"                // No, it isn't.
                "CMP     R2, #0x00\n"                  // It is, check the validity of the partition type
                "CMPNE   R2, #0x80\n"
                "BNE     dg_sd_fat32\n"                // Invalid, go to next partition
                                                       // This partition is valid, it's the first one, bingo!
                "MOV     R4, R12\n"                    // Move the new MBR offset for the partition detection.
                
           "dg_sd_fat32_end:\n"
                // End of DataGhost's FAT32 autodetection code                

                "LDRB    R1, [R4,#0x1C9]\n"
                "LDRB    R3, [R4,#0x1C8]\n"
                "LDRB    R12, [R4,#0x1CC]\n"
                "MOV     R1, R1,LSL#24\n"
                "ORR     R1, R1, R3,LSL#16\n"
                "LDRB    R3, [R4,#0x1C7]\n"
                "LDRB    R2, [R4,#0x1BE]\n"
           //   "LDRB    LR, [R4,#0x1FF]\n"   // replaced, see below
                "ORR     R1, R1, R3,LSL#8\n"
                "LDRB    R3, [R4,#0x1C6]\n"
                "CMP     R2, #0\n"
                "CMPNE   R2, #0x80\n"
                "ORR     R1, R1, R3\n"
                "LDRB    R3, [R4,#0x1CD]\n"
                "MOV     R3, R3,LSL#24\n"
                "ORR     R3, R3, R12,LSL#16\n"
                "LDRB    R12, [R4,#0x1CB]\n"
                "ORR     R3, R3, R12,LSL#8\n"
                "LDRB    R12, [R4,#0x1CA]\n"
                "ORR     R3, R3, R12\n"
           //   "LDRB    R12, [R4,#0x1FE]\n"  // replaced, see below

                "LDRB    R12, [LR,#0x1FE]\n"            // New! First MBR signature byte (0x55)
                "LDRB    LR, [LR,#0x1FF]\n"             //      Last MBR signature byte (0xAA)

                "MOV     R4, #0\n"
                "BNE     loc_FFC3FA80\n"
                "CMP     R0, R1\n"
                "BCC     loc_FFC3FA80\n"
                "ADD     R2, R1, R3\n"
                "CMP     R2, R0\n"
                "CMPLS   R12, #0x55\n"
                "CMPEQ   LR, #0xAA\n"
                "MOVEQ   R6, R1\n"
                "MOVEQ   R5, R3\n"
                "MOVEQ   R4, #1\n"
"loc_FFC3FA80:\n"
                "MOV     R0, #3\n"
                "BL      sub_FFC57D74\n"
                "CMP     R4, #0\n"
                "BNE     loc_FFC3FAB8\n"
                "MOV     R6, #0\n"
                "MOV     R0, R8\n"
                "BL      sub_FFCF8D78\n"
                "MOV     R5, R0\n"
                "B       loc_FFC3FAB8\n"
"loc_FFC3FAA4:\n"
                "MOV     R5, #0x40\n"
                "B       loc_FFC3FAB8\n"
"loc_FFC3FAAC:\n"
                "LDR     R1, =0x365\n"
                "LDR     R0, =0xFFC3F954\n"
                "BL      sub_FFC0BFC8\n"
"loc_FFC3FAB8:\n"
                "STR     R6, [R7,#0x44]!\n"
                "MOV     R0, #1\n"
                "STR     R5, [R7,#4]\n"
                "LDMFD   SP!, {R4-R8,PC}\n"
        );
}; 
